package dfbz.Homework8;

import java.util.concurrent.locks.ReentrantLock;

public class Tage extends Thread {
    private static Integer ticket = 20;
    //定义一个锁
    ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
                lock.lock();
                if(ticket>0) {
                    System.out.println("窗口"+Thread.currentThread().getName()+"正在售出第"+(20-ticket+1)+"张票，"+"剩余:"+(--ticket)+"张");
                }
                lock.unlock();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
